package com.sxc.oa.utils;

import java.sql.*;
import java.util.ResourceBundle;

import static java.util.ResourceBundle.*;
/*
    注册驱动
    获取链接
    获取数据库操作对象
    执行sql语句
    查询处理结果
    释放资源
 */

public class Dbutil {
    //静态变量在类加载的时候执行
    //并且是有顺序的，自上而下的顺序
    //属性资源的绑定
    private static ResourceBundle bundle = getBundle("resource.jdbc");
    private static String driver = bundle.getString("driver");
    private static String url = bundle.getString("url");
    private static String user = bundle.getString("user");
    private static String password = bundle.getString("password");

    static{
        try {
            Class.forName(driver);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
    }
    public static Connection getConnction() throws SQLException {
        Connection conn = DriverManager.getConnection(url,user,password);
        return conn;
    }
    //释放资源
    /*
    conn 连接对象
    ps 数据库操作对象
    rs 结果集对象
     */
    public static void close(Connection conn, Statement ps, ResultSet rs ) throws SQLException {
        if (rs!=null)
        {
            rs.close();
        }
        if(ps!=null){
            ps.close();
        }
        if(conn !=null){
            conn.close();
        }
    }
}
